package containers;
import java.util.*;
import net.mindview.util.*;
public class SlowMap17<K,V> extends AbstractMap<K,V>{
    private List<K> keys=new ArrayList<K>();
    private List<V> values=new ArrayList<V>();
    private EntrySet entries=new EntrySet();
    @Override
    public V put(K key, V value) {
        V oldvalue=get(key);
        if(!keys.contains(key)){
            keys.add(key);
            values.add(value);
        }else
            values.set(keys.indexOf(key),value);
        return oldvalue;
    }

    @Override
    public V get(Object key) {
        if(!keys.contains(key))
            return null;
        return values.get(keys.indexOf(key));
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        return entries;
    }

    public class EntrySet extends AbstractSet<Map.Entry<K,V>>{
        int index;

        @Override
        public int size() {
            return keys.size();
        }

        @Override
        public Iterator<Entry<K, V>> iterator() {
            return new Iterator<Entry<K, V>>() {
                @Override
                public boolean hasNext() {
                    return index<keys.size();
                }

                @Override
                public Entry<K, V> next() {
                    index++;
                    return new MapEntry<K,V>(keys.get(index),values.get(index));
                }

                @Override
                public void remove() {
                    values.remove(index--);
                    keys.remove(index--);
                }
            };
        }
    }

    @Override
    public boolean containsKey(Object key) {
        return keys.contains(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return values.contains(value);
    }

    @Override
    public int hashCode() {
        return this.entrySet().hashCode();
    }

}
